﻿using AntdUI;
using Super.Admin.Global;
using Super.Admin.Utils.ThemeManager;
using Super.Admin.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Super.Admin.Utils.EventAggregator.EventType;
using Super.Admin.Utils.EventAggregator;
using Super.Admin.IServices;
using Super.Admin.Entities;
using Super.Admin.Services;
using Super.Admin.Models.User;
using Super.Admin.Mappers;
using Super.Admin.Models.Role;
using Super.Admin.Models.Department;
using System.Runtime.Intrinsics.Arm;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Super.Admin.Models.Permission;
using System.Security.Policy;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Super.Admin.Views.Page
{
    public partial class MenuManagementModifyButton : UserControl
    {
        private Form _form;
        private List<PermissionBaseModel> _permissionModels;
        private List<PermissionBaseModel> _allPermissionModels;
        private PermissionBaseModel _currentPermissionModel;
        private bool _isCreateChild;
        private bool _isLoad;
        public MenuManagementModifyButton(Form form, List<PermissionBaseModel> permissionModels, List<PermissionBaseModel> allPermissionModels, PermissionBaseModel currentPermissionModel, bool isCreateChild)
        {
            InitializeComponent();
            Dock = DockStyle.Fill;
            _form = form;
            _permissionModels = permissionModels;
            _currentPermissionModel = currentPermissionModel;
            _allPermissionModels = allPermissionModels;
            this.Load += MenuManagementModifyButton_Load;
            _isCreateChild = isCreateChild;
        }

        private void MenuManagementModifyButton_Load(object? sender, EventArgs e)
        {
            _isLoad = true;
            InitializeControls();
            _isLoad = false;
        }
        private void InitializeControls()
        {
            CmbParentMenu.Enabled = true;
            CmbParentMenu.Items.Clear();
            foreach (var dep in _permissionModels)
            {
                var si = new AntdUI.SelectItem(dep.Name, dep.Id) { };
                // 判断是否包含子菜单
                if (dep.PermissionChildren != null && dep.PermissionChildren.Count > 0)
                {
                    SetPermissionSub(si, dep.PermissionChildren);
                }
                CmbParentMenu.Items.Add(si);
            }
            if (_isCreateChild)
            {
                //CmbParentMenu.SelectedValue = _currentPermissionModel.ParentId;
                CmbParentMenu.SelectedValue = _currentPermissionModel.PermissionParent.Name;
                CmbParentMenu.Enabled = false;
            }
            // 初始化控件参数
            else if (_currentPermissionModel.Id != -1)
            {
                TxtName.Text = _currentPermissionModel.Name;
                TxtPermissionKey.Text = _currentPermissionModel.Permissionkey;
                //CmbParentMenu.SelectedValue = _currentPermissionModel.ParentId;
                CmbParentMenu.SelectedValue = _allPermissionModels.FirstOrDefault(a => a.Id == _currentPermissionModel.ParentId).Name;
                BtnSwitchStatus.SelectIndex = BtnSwitchStatus.Items.IndexOf(BtnSwitchStatus.Items.FirstOrDefault(i => int.Parse(i.ID) == _currentPermissionModel.IsActive));
            }
        }
        private void SetPermissionSub(SelectItem si, List<PermissionBaseModel> permissionChildren)
        {
            var subList = new List<AntdUI.SelectItem>();
            // 找到这个ID的所有子菜单
            foreach (var model in permissionChildren)
            {
                var sis = new AntdUI.SelectItem(model.Name, model.Id) { };
                if (model.PermissionChildren != null && model.PermissionChildren.Count > 0)
                {
                    SetPermissionSub(sis, model.PermissionChildren);
                }
                subList.Add(sis);
            }
            si.Sub = new List<object>();
            foreach (var sub in subList)
            {
                si.Sub.Add(sub);
            }
        }
        private void TxtName_TextChanged(object sender, EventArgs e)
        {
            _currentPermissionModel.Name = TxtName.Text;
        }

        private void TxtPermissionKey_TextChanged(object sender, EventArgs e)
        {
            _currentPermissionModel.Permissionkey = TxtPermissionKey.Text;
        }

        private void CmbParentMenu_SelectedValueChanged(object sender, ObjectNEventArgs e)
        {
            if (_isLoad) return;
            if (e.Value == null)
            {
                _currentPermissionModel.ParentId = -1;
                return;
            }
            // 文件夹的父级 只能是文件夹 其他报错
            PermissionBaseModel model = _allPermissionModels.FirstOrDefault(p => p.Id == (int)e.Value);
            if (model == null || model.Type != 1)
            {
                AntdUI.Message.error(_form, "按钮的父级只能是菜单");
                CmbParentMenu.SelectedIndex = -1;
                return;
            }
            _currentPermissionModel.ParentId = (int)e.Value;
        }

        private void BtnSwitchStatus_SelectIndexChanged(object sender, IntEventArgs e)
        {
            _currentPermissionModel.IsActive = int.Parse(BtnSwitchStatus.Items[e.Value].ID);
        }
    }
}